﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using LyData.Data;

public partial class Admin_CodeList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Msg.Visible = false;
        if (!base.IsPostBack)
        {
            this.Bind();
        }
    }

    protected void ArticleList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.ArticleList.PageIndex = e.NewPageIndex;
        this.Bind();
    }

    protected void ArticleList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
    }

    protected void ArticleList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        int num = e.Row.DataItemIndex - (this.ArticleList.PageIndex * this.ArticleList.PageSize);
        if (num >= 0)
        {
            string str = this.ArticleList.DataKeys[num].Values[1].ToString();
            Button button = (Button)e.Row.FindControl("DelBt");
            HyperLink link = (HyperLink)e.Row.FindControl("EditHyperLink");
            if (button != null)
            {
                if (!ManagerLogin.ValidateRole(string.Format("1_{0}_b", str)))
                {
                    button.Enabled = false;
                }
                if (!ManagerLogin.ValidateRole(string.Format("1_{0}_a", str)))
                {
                    link.Enabled = false;
                }
            }
        }
    }

    protected void ArticleList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int rowIndex = e.RowIndex;
        string str = this.ArticleList.DataKeys[rowIndex].Values[0].ToString();
        IDb db = new DbFactory().DriverDb();
        db.Open();
        db.Delete("codeList", string.Format("cId={0}", str));
        db.Close();
        this.Bind();
        this.Msg.Text = "图片删除成功！";
        this.Msg.Visible = true;
    }

    protected void ArticleList_RowEditing(object sender, GridViewEditEventArgs e)
    {
    }

    protected void ArticleList_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
    }

    protected void ProductsLock_CheckedChanged(object sender, EventArgs e)//推荐
    {
        IDb db = new DbFactory().DriverDb();
        db.Open();
        CheckBox ckeSelect = null;
        for (int i = 0; i < ArticleList.Rows.Count; i++)
        {
            ckeSelect = (CheckBox)ArticleList.Rows[i].FindControl("ProductsLock");
            string str = (ckeSelect.Checked == true ? "True" : "False");
            string strwhere = string.Format("cId ={0}", ArticleList.Rows[i].Cells[0].Text);
            string[,] includeColumns = new string[,] { { "isOk", str, "bit", "0" } };
            db.Update("CodeList", includeColumns, strwhere);
        }
        db.Close();
        this.Bind();
    }

    private void Bind()
    {
        string strwhere = "1=1";
        
        IDb db = new DbFactory().DriverDb();
        db.Open();
        DataTable table = db.DataTable("*", "CodeList", strwhere, "codeClassid Asc,Cid Desc");
        db.Close();

        ArticleList.DataSource = table;
        ArticleList.DataBind();
    }
}
